<?php

namespace app\enumerations;

trait EnumTrait
{

    static function getDictionaryCode() {

        return self::DICTIONARY_CODE;

    }


    /**
     * 获取枚举list
     * @return array
     */
    static public function getAllList()
    {
        $r = new \ReflectionClass(self::class);

        $constantsList = $r->getConstants();

        $list = [];
        $info = [];
        $index = 1;
        foreach ($constantsList as $key => $value) {

            if($key == "DICTIONARY_CODE") {
                continue;
            }

            if($index % 3 == 1) {
                $info["value"] = $value;
            } else if($index % 3 == 2) {
                $info["code"] = $value;
            } else if($index % 3 == 0) {
                $info["desc"] = $value;
                array_push($list, $info);
            }

            $index++;

        }

        return $list;

    }

    /**
     * 根据枚举code获得对应值
     * @param $key
     * @return string
     */
    static public function getEnumValue($key) {

        $list = self::getAllList();
        $value = "";

        foreach ($list as $item) {
            if($item["code"] == $key) {
                $value = $item["value"];
                break;
            }
        }

        return $value;

    }


    /**
     * 根据枚举value获取描述值
     * @param $value
     * @return string
     */
    static public function getEnumString($value) {

        $list = self::getAllList();
        $str = "";

        foreach ($list as $item) {
            if($item["value"] == $value) {
                $str = $item["desc"];
                break;
            }
        }

        return $str;

    }
    

}



















